//
//  QDTextListViewController.m
//  QDMetroClient
//
//  Created by YueWen on 2019/2/18.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDTextListViewController.h"
#import "QDLineTextTableViewCell.h"
#import "QDMetroClient-Swift.h"
#import "UITableViewCell+QDTextListModelUpdater.h"

@interface QDTextListViewController ()

@end

@implementation QDTextListViewController


- (void)loadPropertysAtInitialization {
    [super loadPropertysAtInitialization];
    
    self.currentPage = 0;
    self.totalPage = 0;
    self.url = [NSString stringWithFormat:@"%@/ngstatic/document/getDocumentList",QDBaseAppUrl];
    self.type = @"8";
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    //开始请求所有的消息
    [self configRequestAPI];
    
    // 如果是自动加载
    if (self.autoRequestInViewDidLoad) {
       [self loadData];
    }
}


- (BOOL)autoRequestInViewDidLoad { return true; }
- (void)customDataWithInfoWhenReset:(NSDictionary *)info originInfo:(nonnull NSDictionary *)origin { }
- (void)customDataWithInfoWhenDefault:(NSDictionary *)info originInfo:(nonnull NSDictionary *)origin { }


/// 配置requestAPI
- (void)configRequestAPI {
    
    self.requestManager = QDNetWorkingAPI.new;
    self.requestManager.url = self.url;
    self.requestManager.method = HTTP_POST;
    self.requestManager.delegate = self;

}

/// 全部刷新数据
- (void)loadData {

    self.currentPage = 0;
    self.totalPage = 0;
    self.requestManager.requestKey = NWNetWorkingAPIKeyReset;
    
    if (self.type){
       self.requestManager.parameters = @{@"type":self.type,@"page":@(self.currentPage),@"size": @10};
    }else {
        self.requestManager.parameters = @{@"page":@(self.currentPage),@"size": @10};
    }
    
    [self.requestManager resumeNetworking];
}


/// 加载更多数据
- (void)loadMoreData {
    
    if (self.type){
        self.requestManager.parameters = @{@"type":self.type,@"page":@(self.currentPage),@"size": @10};
    }else {
        self.requestManager.parameters = @{@"page":@(self.currentPage),@"size": @10};
    }
    [self.requestManager resumeNetworking];
}



- (void)headerRefreshBeginHandler {
    [self.refreshFooter resetNoMoreData];
    [self loadData];
}


- (void)footerRefreshBeginHandler {
    [self loadMoreData];
}



#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.hasDatas ? 1 : 0;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [self tableViewCellForTableViewAtIndexPath:indexPath];
    //更新数据
    [cell textUpdateByData:self.datas[indexPath.row] atIndexPath:indexPath delegate:self associateObject:nil];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[self tableViewCellForTableViewAtIndexPath:indexPath] textUpdaterHeightByData:nil atIndexPath:indexPath];
}



- (UITableViewCell *)tableViewCellForTableViewAtIndexPath:(NSIndexPath *)indexPath {
    return [self.tableView ritl_dequeueReusableCellWithIdentifier:[self identifierForCellAtIndexPath:indexPath]
                                                            class:[self classForCellAtIndexPath:indexPath]];
}


- (NSString *)identifierForCellAtIndexPath:(NSIndexPath *)indexPath {
    return @"QDLineTextTableViewCell";
}

- (Class)classForCellAtIndexPath:(NSIndexPath *)indexPath {
    return QDLineTextTableViewCell.class;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QDTextListModel *textModel = self.datas[indexPath.row];
    if (![textModel isKindOfClass:QDTextListModel.class]){ return; }

    [self pushToShareWebViewControllerWithUrl:textModel.webUrl shareTitle:textModel.title shareImageUrl:nil shareDesc:textModel.author autoTitle:false title:@"详情" id:textModel.identifier qdmetro:true];
}



#pragma mark - <NWNetWorkingAPIAction>

- (void)networkingAPIWillRequest:(NWNetWorkingAPI *)networkingAPI {
    //如果是刷新，不需要hud
    if (!(self.refreshHeader.isRefreshing || self.refreshFooter.isRefreshing)) {
        ShowWaitingHUD(self.view);
    }
}


- (void)networkingAPIEndRequest:(NWNetWorkingAPI *)networkingAPI {
    //如果是刷新，不需要hud
    if (!(self.refreshHeader.isRefreshing || self.refreshFooter.isRefreshing)) {
        DismissMBHUD(self.view); return;
    }
    
    [self endHeaderRefreshing];
    [self endFooterRefreshing];
}


- (void)networkingAPI:(NWNetWorkingAPI *)networkingAPI completeWithData:(NSDictionary *)info {
    
    //存放数据的位置
    NSDictionary *result = [info valueForKey:@"data"];
    
    if ([networkingAPI.requestKey isEqualToString:NWNetWorkingAPIKeyReset]) {//如果是第一次或者下拉刷新
        
        networkingAPI.requestKey = NWNetWorkingAPIKeyDefault;
        [self customDataWithInfoWhenReset:result originInfo:info];
        [self.datas removeAllObjects];
    }
    
    //获得所有的资讯并整体转换
    [self.datas addObjectsFromArray:[NSArray yy_modelArrayWithClass:QDTextListModel.class json:result]];
    [self customDataWithInfoWhenDefault:result originInfo:info];
    
    //当前页码+1
    self.currentPage += 1;
    
    if (result.count < 10) {//如果小于10，表示没有数据
         [self.refreshFooter endRefreshingWithNoMoreData];
    }
    
    //刷新即可
    [self.tableView reloadData];
    
    //如果没有数据
    if (self.datas.count == 0){
        
        [self showNothingView:true];
        [self setNavigationBarShadowViewHidden:true];
//        ShowMBHudwithText(@"无更多数据", self.view);
        
    }else {
        [self showNothingView:false];
        [self setNavigationBarShadowViewHidden:false];
    }
}


- (void)networkingAPI:(NWNetWorkingAPI *)networkingAPI fail:(NSError *)error info:(NSDictionary *)info {
    ShowMBHudwithText(error.localizedDescription, self.view);
}



#pragma mark - Property

- (NSMutableArray<QDTextListModel *> *)datas {
    if (!_datas) {
        _datas = [NSMutableArray arrayWithCapacity:10];
    }
    return _datas;
}


- (BOOL)hasDatas {
    return self.datas.count > 0;
}

@end
